from django.shortcuts import render
from rest_framework.views import Response,APIView
from .sers import *


class AddCate(APIView):
    def get(self,request):
        cate1 = Cate.objects.filter(parent_id=None)
        ser = CateSer(cate1,many=True)
        return Response(ser.data)

    def post(self,request):
        # name: "",
        # parent: "",
        # group: "",
        name = request.data.get("name")
        parent = request.data.get("parent")
        group = request.data.get("group")
        Cate.objects.create(name=name,parent_id=parent,group_id=group)
        return Response({"msg":"添加成功"})


class Index(APIView):
    def get(self,request):
        data = {}
        data["cate_list"]=[]
        data["goods_list"]=[]
        groups = CateGroup.objects.all()
        for group in groups:
            # 获取所有一级标签
            temp={}
            cate1_list=Cate.objects.filter(group_id=group.id,level=1)
            temp["cate1_list"]=CateSer(cate1_list,many=True).data
            # 获取一级分类下的二级分类
            cate2_list = Cate.objects.filter(group_id=group.id, level=2)
            temp["cate2_list"] = Cate2Ser(cate2_list, many=True).data

            data["cate_list"].append(temp)
        # 查询前三个一级分类
        top3_cate = Cate.objects.all()[0:3]
        for item in top3_cate:
            temp = {}
            temp["cate"]=CateSer(item).data
            temp["tags"]=["时尚新品","加价换购","畅想低价","手机配件","精选单品"]
            temp["goods"]=[]
            data["goods_list"].append(temp)
        return Response(data)



